package com.example.smartcity_poverty.ui.AllService.GovernmentHotLine.Activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.GovernmentHotLine.GmHotLineUserListAllModel
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_gm_hot_line_show_list.*
import kotlinx.android.synthetic.main.re_gmhotline_me_list_all.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 *  诉求列表页面
 */
class GmHotLineShowListActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_gm_hot_line_show_list)
        title = "诉求列表页面"

        val id = intent.getStringExtra("id")?.toInt()
        id?.let {
            // 填充数据
            setIdShow(it)
        }

        id?.let {
            val a: Int = it
            // 新建诉求
            addGmHotLine.setOnClickListener {
                startActivity(Intent(this, AddGmHotLineActivity::class.java).apply {
                    putExtra("id", a.toString())
                })
            }
        }



    }

    /**
     * 诉求列表
     */
    private fun setIdShow(id: Int) {
        if (getToken().isNotEmpty()) {
            CoroutineScope(Dispatchers.Main).launch {
                api.getGmHotLineUserListAll(getToken(), id).apply {
                    if (this.code == 200) {
                        val rows = this.rows
                        // 填充数据
                        runOnUiThread {
                            gmShowListRv.apply {
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter =
                                    ShowListAdapter(ArrayList<GmHotLineUserListAllModel.RowsBean>().apply {
                                        this.addAll(rows!!)
                                    }, R.layout.re_gmhotline_me_list_all)
                                this.isNestedScrollingEnabled = false
                            }
                            "为你找到了${rows?.size}诉求".show()
                        }

                    } else runOnUiThread { "加载诉求出错！".show() }
                }
            }
        } else "请先登录".show()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }


    companion object {
        class ShowListAdapter(
            val list: MutableList<GmHotLineUserListAllModel.RowsBean>,
            val layoutId: Int
        ) : BaseRecyclerAdapter<GmHotLineUserListAllModel.RowsBean>(list, layoutId) {
            override fun setData(
                view: BaseViewHolder.ViewFind,
                data: GmHotLineUserListAllModel.RowsBean,
                position: Int,
                holder: BaseViewHolder
            ) {
                list.sortByDescending { it.createTime }

                val d = list[position]
                val fv = holder.viewFind
                val con = fv.context

                fv.gmMeTitle.text = d.title
                fv.gmMeUser.text = "承办单位:${d.undertaker}"
                if (d.state.toString() == "1") fv.gmMeIsOk.text = "处理状态:已处理"
                else fv.gmMeIsOk.text = "处理状态: 处理中"
                fv.gmMeTime.text = "提交时间: ${d.createTime}"

                holder.itemView.setOnClickListener {
                    // 点击进入诉求详情页
                    holder.viewFind.view?.context?.startActivity(Intent(
                        holder.viewFind.view?.context,
                        ShowGmHotLineUserAllActivity::class.java
                    ).apply {
                        putExtra("title", d.title.toString())
                        putExtra("image", d.imgUrl.toString())
                        putExtra("undertaker", d.undertaker.toString())
                        putExtra("state", d.state.toString())
                        putExtra("createTime", d.createTime.toString())
                        putExtra("detailResult", d.detailResult.toString())
                        putExtra("contend", d.content.toString())
                    })
                }
            }

        }
    }
}